package com.rhino.xchat

import com.rhino.base.constant.BaseFlowMsg
import com.rhino.base.provider.UserAccountProvider
import com.rhino.base.utils.FlowUtils
import com.rhino.xchat.constant.Cache
import com.rhino.xchat.http.response.ResLoginInfo
import com.rhino.xchat.http.response.ResUserInfo
import com.therouter.inject.ServiceProvider


/**
 * @author LuoLin
 * @since Create on 2024/9/29.
 */
@ServiceProvider
class UserAccountProviderImpl : UserAccountProvider {

    override suspend fun saveLoginInfo(any: Any?) {
        Cache.saveLoginInfo(any as? ResLoginInfo)
        FlowUtils.sendValue(BaseFlowMsg.LOGIN_STATUS, any != null)
    }

    override suspend fun saveUserInfo(any: Any?) {
        Cache.saveUserInfo(any as? ResUserInfo)
        FlowUtils.sendValue(BaseFlowMsg.USER_INFO_CHANGE, Cache.getUserInfo())
    }

    override suspend fun clearAllUserInfoSync() {
        Cache.saveLoginInfo(null)
        Cache.saveUserInfo(null)
        Cache.clearAllUserInfo()
        FlowUtils.sendValue(BaseFlowMsg.LOGIN_STATUS, false)
    }

    override fun hasLogin(): Boolean {
        return Cache.hasLogin()
    }
}